/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shattered2;

import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 *
 * @author Peer
 */
public class PiecesButton extends JButton implements Comparable {

    static final int TOP = 1, LEFT = 2, BOTTOM = 3, RIGHT = 4;
    private int difference = 0;
    private Integer[] otherColors = null, myColors = null;

    
    public int getDifference() {
        return difference;
    }
    
    public PiecesButton(PuzzlePiece piece, Integer[] colors, int direction) {
        super(new ImageIcon(piece.getImage()));
        if(colors != null){
            this.otherColors = colors;
            this.myColors = PiecesWindow.getBorderColors(piece, direction);
            for (int i = 0; i < otherColors.length; i++) {
                difference+=Math.abs((otherColors[i]&0xFF)-(myColors[i]&0xFF));
                difference+=Math.abs(((otherColors[i]>>8)&0xFF)-((myColors[i]>>8)&0xFF)); 
                difference+=Math.abs(((otherColors[i]>>16)&0xFF-(myColors[i>>16])&0xFF)); 
            }
        }
    }

    @Override
    public int compareTo(Object o) {
        PiecesButton otherButton = (PiecesButton)o;
        if(this.difference > otherButton.getDifference()){
            return 1;
        }
        if(this.difference < otherButton.getDifference()){
            return -1;
        }
        return 0;
    }


    
    
    
    
    
}
